﻿using BitMiracle.LibTiff.Classic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ELTE.SEM.Data.Connectors
{
    public class TiffConnector
    {
        public static SemDataParsedResult GetImageData(MemoryStream stream, string name)
        {
            SemDataParsedResult parseResult = null;
            try
            {
                if (stream != null && stream.Length > 0)
                {
                    stream.Position = 0;
                    //using (Tiff image = Tiff.Open(path, "r"))
                    using (Tiff image = Tiff.ClientOpen(name, "r", stream, new TiffStream()))
                    {
                        for (int fieldId = 31000; fieldId < 65535; fieldId++)
                        {
                            FieldValue[] value = image.GetField((TiffTag)fieldId);
                            if (value != null)
                            {
                                if (value.Length == 2)
                                {
                                    string valueAsString = value[1].ToString();
                                    if (valueAsString.Contains('['))
                                    {
                                        parseResult = valueAsString.ParseSemData();
                                        if (parseResult.UnknownRecords.Count > 0)
                                        {
                                            string ukrecords = String.Format("Ismeretlen rekordok . Adat : {0}", String.Join("#", parseResult.UnknownRecords.ToArray()));
                                        }
                                        Func<BaseSemData, bool> cond = baseData => baseData.UnknownValues.Count > 0;
                                        if (parseResult.BaseSemData.Any(cond))
                                        {
                                            parseResult.BaseSemData.Where(cond).ToList().ForEach(unk =>
                                            {
                                                string ukValues = String.Format("Ismeretlen értékek {0} osztályban: {1}", unk.Title, String.Join("#", unk.UnknownValues.ToArray()));
                                            });
                                        }
                                    }
                                    else
                                    {
                                        //Console.WriteLine("NO ']' DATA found. ");
                                    }
                                }
                                else
                                {

                                }
                            }
                            else
                            {
                                //Console.WriteLine("No field for field id " + fieldId.ToString());
                            }
                        }
                        //Console.ReadKey();
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return parseResult;
        }

        public static string GetImageDataAsString(MemoryStream stream, string name)
        {
            string result = String.Empty;
            try
            {
                if (stream != null && stream.Length > 0)
                {
                    stream.Position = 0;
                    //using (Tiff image = Tiff.Open(path, "r"))
                    using (Tiff image = Tiff.ClientOpen(name, "r", stream, new TiffStream()))
                    {
                        for (int fieldId = 31000; fieldId < 65535; fieldId++)
                        {
                            FieldValue[] value = image.GetField((TiffTag)fieldId);
                            if (value != null)
                            {
                                if (value.Length == 2)
                                {
                                    string valueAsString = value[1].ToString();
                                    result = valueAsString;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
 
            }
            return result;
        }
    }
}
